/*
运算符重载
   1.实验题目
    设计Complex类，能够实现各种运算符重载
	2.实验目的
   熟悉各种运算符重载的用法
   3.要求：
    1、具有+，-，×，++，--, =功能
     2、能够和整数实现相互转换
     3、直接对复数对象完成输入输出操作
*/

#include <iostream>

using namespace std;

class Complex {
	private:
		double re, im;

	public:
		Complex(double r=0, double i=0): re(r), im(i) {}
		~Complex() {
			cout << "Deconstructing ..." << endl;
		}
		/*
		Complex(const Complex & obj) {
			re = obj.re;
			im = obj.im;
		}*/
		Complex operator + (const Complex &);
		Complex operator - (const Complex &);
		Complex operator * (const Complex &);
		Complex & operator ++ (); // 前
		Complex operator ++ (int); // 后
		Complex & operator -- ();
		Complex operator -- (int);
		Complex & operator = (const Complex &);
		friend istream & operator >> (istream &, Complex &);
		friend ostream & operator << (ostream &, const Complex &);

		// 强制类型转换
		// 将类类型转换为整形
		operator int ();
};

Complex Complex::operator + (const Complex & obj) {
	Complex temp;
	temp.re = re + obj.re;
	temp.im = im + obj.im;

	return temp;
}

Complex Complex::operator - (const Complex & obj) {
	Complex temp;
	temp.re = re - obj.re;
	temp.im = im - obj.im;

	return temp;
}

Complex Complex::operator * (const Complex & obj) {
	Complex temp;
	temp.re = re * obj.re;
	temp.im = im * obj.im;

	return temp;
}

// 前++
Complex & Complex::operator ++() {
	re++;
	im++;
	
	return *this;
}

Complex Complex::operator ++(int ) {
	Complex temp(*this);
	re++;
	im++;

	return temp;
}

Complex & Complex::operator --() {
	re--;
	im--;

	return *this;
}

Complex Complex::operator --(int) {
	Complex temp(*this);
	re++;
	im++;

	return temp;
}

Complex & Complex::operator = (const Complex & obj) {
	re = obj.re;
	im = obj.im;

	return * this;
}

Complex::operator int () {
	return re;
}

istream & operator >> (istream & stream, Complex & obj) {
	stream >> obj.re >> obj.im;
	
	return stream;
}

ostream & operator << (ostream & stream, const Complex & obj) {
	stream << obj.re;
	if(obj.im >= 0)
		stream << "+";
	stream << obj.im << "i";

	return stream;
}

int main(int argc, char * argv[]) {
	Complex obj1(11, 7);
	Complex obj2(obj1), obj3;

	obj3 = obj1;

	cout << "Obj1: " << obj1 << endl;
	cout << "Obj1++: " << obj1++ << endl;
	cout << "++Obj1: " << ++obj1 << endl;
	cout << "Obj2(obj1): " << obj2 << endl;
	cout << "Obj2--: " << obj2-- << endl;
	cout << "--Obj2: " << --obj2 << endl;
	cout << "Obj3 = obj1: " << obj3 <<endl;
	cout << "int(Obj3): " << int(obj3) <<endl;

	Complex obj4(3, 5), obj5(6, 3);
	Complex obj6;
	obj6 = obj4 * obj5;
	cout << "obj6 = obj3(3,5)*obj6(6,3): " << obj6 << endl;
	obj6 = obj4 + obj5;
	cout << "obj6 = obj3(3,5)+obj6(6,3): " << obj6 << endl;
	obj6 = obj4 - obj5;
	cout << "obj6 = obj3(3,5)-obj6(6,3): " << obj6 << endl;
	
	return 0;
}
